package com.xinyues.gamemanager.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.xinyues.gamemanager.common.GmConstants;
import com.xinyues.gamemanager.dao.entity.GmAccount;
import com.xinyues.gamemanager.dao.entity.GmRole;
import com.xinyues.gamemanager.params.AddAccountParam;
import com.xinyues.gamemanager.params.ChangeAccountRole;
import com.xinyues.gamemanager.service.CommonService;
import com.xinyues.gamemanager.service.GmAccountService;
import com.xinyues.gamemanager.service.GmRoleService;
import com.xinyues.gamemanager.vo.VoAccount;

@RestController
@RequestMapping("account")
public class GmAccountController {
    @Autowired
    private GmAccountService gmAccountService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private GmRoleService roleService;
    private Logger logger = LoggerFactory.getLogger(GmAccountController.class);

    @RequestMapping("addAccount")
    public Object addAccount(@RequestBody AddAccountParam accountParam, HttpServletRequest request) {
        if (!accountParam.getPassword1().equals(accountParam.getPassword2())) {
            return commonService.error(-1, "两次输入的密码不一样");
        }
        GmAccount creator = gmAccountService.getCurrentAccount();
        gmAccountService.addGmAccount(accountParam.getUsername(), accountParam.getPassword1(), creator.getUsername());
        logger.debug("添加用户成功：{}", accountParam);
        return commonService.response("添加成功");
    }

    @RequestMapping("deleteAccount/{accountId}")
    public Object deleteAccount(@PathVariable("accountId") long accountId) {
        gmAccountService.deleteAccount(accountId);
        return commonService.response();
    }

    @RequestMapping("getAccount")
    public Object getAccount(@RequestParam("page") int page, @RequestParam("rows") int rows, HttpServletRequest request) {
        List<GmAccount> gmAccounts = gmAccountService.getAllGmAccount();
        gmAccounts.forEach(a -> {
            a.setPassword("*************");
        });
        List<VoAccount> voAccounts = gmAccounts.stream().filter(g -> !g.getUsername().equals(GmConstants.ADMIN)).map(g -> {
            VoAccount voAccount = new VoAccount();
            BeanUtils.copyProperties(g, voAccount);
            if (g.getRoles().size() > 0) {
                GmRole gmRole = roleService.getRole(g.getRoles().get(0));
                voAccount.setRole(gmRole.getText());
            }
            return voAccount;
        }).collect(Collectors.toList());

        return commonService.responseTable(voAccounts, voAccounts.size());
    }

    @PostMapping("updateAccountRole")
    public Object updateAccountRole(@RequestBody ChangeAccountRole changeAccountRole) {
        gmAccountService.updateAccountRole(changeAccountRole);
        return commonService.response();
    }

    @PostMapping("updatePass")
    public Object updatePassword(@RequestParam String password, HttpServletRequest request) {
        GmAccount account = gmAccountService.getCurrentAccount();
        if (account != null) {
            account.setPassword(password);
            boolean result = gmAccountService.updateAccountRole(account);
            if (result) {
                return commonService.response();
            }
        }
        return commonService.error(-1, "更新失败，退出重新登陆再试");
    }



}
